package com.example.shopcardemo;
 
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.shopcardemo.ChangNumberView.AddNumberListener;
import com.example.shopcardemo.ChangNumberView.CutNumberListener;
 

public class MainActivity extends  Activity {
	ChangNumberView cnvView;
	ImageView carBgIv;
	TextView  numberTv;
	
	ArrayList<PurchaseNewBean> purchaseNewList;
	PurchaseNewBean newBean;
	private int productNumber;
	private ShopingCar shopingCar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);
		initData();
		initView();
	}
	
	private void initData()
	{
		newBean = new PurchaseNewBean();
		purchaseNewList = new ArrayList<PurchaseNewBean>();
		
		newBean.setProduct_id("123");
		newBean.setProduct_name("香蕉");
		newBean.setUnit_price(23.23);
		newBean.setNumber("0");
		purchaseNewList.add(newBean);
	}

	@Override
	public void onPause() {
		super.onPause();
		if(shopingCar!=null&&shopingCar.getAllGoods().size()>0){
			shopingCar.productNumber=productNumber;
			AppContext.getApplicationBundle().putParcelable(ShopingCar.SERIALIZABLE_KEY, shopingCar);
		}
	}
	
	private void initView()
	{
		numberTv = (TextView)findViewById(R.id.tv_purchase_new_car_number);
		carBgIv = (ImageView)findViewById(R.id.iv_purchase_new_car_bg);
		carBgIv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				shopingCar.productNumber=productNumber;
				AppContext.getApplicationBundle().putParcelable(ShopingCar.SERIALIZABLE_KEY, shopingCar);
				
				
				Intent intent = new Intent(MainActivity.this,SecondActivity.class);
				startActivity(intent);
				finish();
			}
		});
		
		cnvView = (ChangNumberView)findViewById(R.id.cnv_purchase_new_item_number);
		cnvView.setAddNumberListener(new AddNumberListener() {

			@Override
			public void onAdd(int currentNumber) {
				newBean.setNumber(String.valueOf(Integer.valueOf(newBean.getNumber())+1));
				shopingCar.addGood(newBean);
				productNumber++;
				refreshCarNumber();
				executeAddProductAnimation();
			}
		});
		cnvView.setCutNumberListener(new CutNumberListener() {

			@Override
			public void onCutFinish() {
				newBean.setNumber(String.valueOf(0));
				shopingCar.removeGood(newBean);
				if(productNumber==0){
					shopingCar.clearAll();;
				}
			}

			@Override
			public void onCut(int currentNumber) {
				newBean.setNumber(String.valueOf(currentNumber));
				productNumber--;
				refreshCarNumber();
			}
		});

		
		shopingCar=new ShopingCar();

		//有购物车缓存则取缓存
		ShopingCar shopingCarCache=AppContext.getApplicationBundle().getParcelable(ShopingCar.SERIALIZABLE_KEY);
		//有购物车缓存则初始化数据
		if(shopingCarCache!=null&&shopingCarCache.getAllGoods().size()>0){
			for (int i = 0; i < purchaseNewList.size(); i++) {
				for (int j = 0; j < shopingCarCache.getAllGoods().size(); j++) {
					System.out.println("shopingCarCache.getAllGoods().get(j)"+shopingCarCache.getAllGoods().get(j).getProduct_id());
					System.out.println("purchaseNewList.get(i)"+purchaseNewList.get(i).getProduct_id());

					if(shopingCarCache.getAllGoods().get(j).getProduct_id().equals(purchaseNewList.get(i).getProduct_id())){
						purchaseNewList.get(i).setNumber(shopingCarCache.getAllGoods().get(j).getNumber());
						shopingCar.addGood(purchaseNewList.get(i));
					}
				}
			}
			productNumber=shopingCarCache.productNumber;
			cnvView.setCurrentNumber(productNumber);
			refreshCarNumber();
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	/**
	 * 执行购物车添加商品图标动画
	 */
	public void executeAddProductAnimation(){
		ScaleAnimation sa=new ScaleAnimation(1.0f, 1.2f, 1.0f, 1.2f, 
				ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
		sa.setDuration(200);
		carBgIv.setAnimation(sa);

		sa.startNow();
	}
	/**
	 * 清空购物车
	 */
	private void clearShopCar(){
		for(PurchaseNewBean bean : shopingCar.getAllGoods()){
			bean.setNumber("0");
		}
		shopingCar.clearAll();

		productNumber=0;

		refreshCarNumber();

	}
	
	
	private void refreshCarNumber(){
		if(productNumber<0){
			productNumber=0;
		}
		numberTv.setText(String.valueOf(productNumber));
		if(productNumber==0){
			numberTv.setVisibility(View.INVISIBLE);
			carBgIv.setImageResource(R.drawable.shop_car_null);
		}else{
			numberTv.setVisibility(View.VISIBLE);
			carBgIv.setImageResource(R.drawable.shop_car);
		}
	}
 
}
